<?php

class Auth
{
    public function login()
    {
        $redirectUrl=$this->getRedirectUrl();
        $requestNum=Yii::app()->request->getQuery('requestNum');

        return $this->$requestNum($redirectUrl);
    }

    public function getImgLink($route)
    {
        $img=Yii::app()->getBaseUrl(true).'/protected/extensions/snauth/image/'.$this->getServiceName().'_auth_img.png';

        $url=Yii::app()->createAbsoluteUrl($route,array(
            'service'=>$this->getServiceName(),
            'redirectUrl'=>str_replace('/','.',$route),
            'requestNum'=>'firstRequest',
        ));

        return $this->getImg($url, $img, 'Вход через '.$this->getServiceName(), 'Войти через аккаунт '.$this->getServiceName());
    }

    protected function getImg($href, $src, $alt, $title)
    {
        return '<a href="'.$href.'"><img src="'.$src.'" alt="'.$alt.'" title="'.$title.'" /></a>';
    }

    protected function getRedirectUrl()
    {
        return str_replace('.','/',Yii::app()->request->getQuery('redirectUrl'));
    }

    protected function getReturnUrl()
    {
        $route=$this->getRoute();
        return Yii::app()->createAbsoluteUrl($route,array(
            'service'=>$this->getServiceName(),
            'redirectUrl'=>str_replace('/','.',$route),
            'requestNum'=>'secondRequest',
        ));
    }

    protected function getRoute()
    {
        $route='';
        if(Yii::app()->controller->module->id) $route.=Yii::app()->controller->module->id;
        if(Yii::app()->controller->id) $route.='/'.Yii::app()->controller->id;
        if(Yii::app()->controller->action->id) $route.='/'.Yii::app()->controller->action->id;

        return $route;
    }

    protected function quit($error){
        Yii::app()->user->setFlash('userMessage','Попытка аутентификации не удалась.<br />Error:<br />'.$error);
        Yii::app()->request->redirect(Yii::app()->createUrl('site/index'));
    }
}